# -*- coding: utf-8 -*-
"""
Created on Thu Jul 27 09:56:42 2017

@author: ZFH
"""
import json
from collections import defaultdict
from collections import Counter
import pandas as pd
import numpy as np
from pandas import DataFrame,Series

file_name = r'C:\Users\ZFH\Desktop\data.txt'

records = [json.loads(line) for line in open(file_name)]

time_zone = [rec['tz'] for rec in records if 'tz' in rec]
# 计数函数，对时区进行统计
def get_counts(time_zone):
    count_time_zone = defaultdict(int)
    for i in time_zone:
        count_time_zone[i] += 1
    return count_time_zone

# count_time_zone = get_counts(time_zone)
count_time_zone = Counter(time_zone)
# count_time_zone = sorted(count_time_zone.items(),key=lambda d:d[1], reverse=True)

frame = DataFrame(records)
# 缺失值用miss替代
clear_tz = frame['tz'].fillna('Missing')
# 空值用unkown替代
clear_tz[clear_tz == ''] = 'Unkown'
tz_count = clear_tz.value_counts()
# tz_count[:10].plot(kind='barh',rot=0)

results = Series(i.split()[0] for i in frame.a.dropna())
results_count = results.value_counts()

cframe = frame[frame.a.notnull()]

opera_system = np.where(cframe['a'].str.contains('Windows'),'Windows','not Windows')

by_tz_os = cframe.groupby(['tz', opera_system])

agg_count = by_tz_os.size().unstack().fillna(0)

indexer = agg_count.sum(1).argsort()

count_subset = agg_count.take(indexer)[-10:]

# count_subset[:10].plot(kind='barh',stacked=True)

normed_subset = count_subset.div(count_subset.sum(1),axis=0)

#　normed_subset.plot(kind='barh',stacked=True)